﻿using EntityPet;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using PET.Tools;
using Tools;

namespace PET.Controllers
{
    /// <summary>
    /// 宠物保险项控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class InsCoverController : ControllerBase
    {
        private readonly PetDbContext petC;
        public InsCoverController(PetDbContext db)
        {
            petC = db;
        }

        [HttpGet("inc")]
        public List<Pet_InsCover> Getins()
        {
            return petC.Pet_InsCover.ToList();
        }

        [HttpDelete("Del")]
        public bool Get(Guid id)
        {
            Pet_InsCover? insc = petC.Pet_InsCover.Where(m => m.InsId == id).SingleOrDefault();
            if (insc != null)
                petC.Pet_InsCover.Remove(insc);
            int result = petC.SaveChanges();
            return result > 0;

        }

        [HttpGet("id")]
        public Result GetId(Guid id)
        {
            Result result = new Result();
            result.Data = petC.Pet_InsCover.Where(w => w.InsId == id).SingleOrDefault();
            return result;
        }

       
        [HttpPost]
        public Result AddPet([FromBody] Pet_InsCover cover)
        {
            Result result = new Result();
            petC.Pet_InsCover.Add(cover);
            result.Data = petC.SaveChanges();
            return result;
        }

        [HttpPut]
        public Result Put([FromBody] Pet_InsCover cover)
        {
            Result result = new Result();
            petC.Pet_InsCover.Update(cover);
            result.Data = petC.SaveChanges();
            return result;
        }
    }
}
